fix(replay): Post checkCanRecord to main thread to prevent deadlock - #5837
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📲 Install BuildsAndroid
|
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 05aa61d | 326.06 ms | 385.46 ms | 59.40 ms |
| bb0ff41 | 315.84 ms | 350.76 ms | 34.92 ms |
| 806307f | 357.85 ms | 424.64 ms | 66.79 ms |
| d501a7e | 307.33 ms | 341.94 ms | 34.61 ms |
| 0ee65e9 | 321.06 ms | 361.24 ms | 40.18 ms |
| ed33deb | 334.19 ms | 362.30 ms | 28.11 ms |
| 9fbb112 | 401.87 ms | 515.87 ms | 114.00 ms |
| b8bd880 | 314.56 ms | 336.50 ms | 21.94 ms |
| 5b1a06b | 315.40 ms | 353.33 ms | 37.94 ms |
| 6edfca2 | 316.43 ms | 398.90 ms | 82.46 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 05aa61d | 0 B | 0 B | 0 B |
| bb0ff41 | 0 B | 0 B | 0 B |
| 806307f | 1.58 MiB | 2.10 MiB | 533.42 KiB |
| d501a7e | 0 B | 0 B | 0 B |
| 0ee65e9 | 0 B | 0 B | 0 B |
| ed33deb | 1.58 MiB | 2.13 MiB | 559.52 KiB |
| 9fbb112 | 1.58 MiB | 2.11 MiB | 539.18 KiB |
| b8bd880 | 1.58 MiB | 2.29 MiB | 722.92 KiB |
| 5b1a06b | 0 B | 0 B | 0 B |
| 6edfca2 | 1.58 MiB | 2.13 MiB | 559.07 KiB |
Previous results on branch: rz/fix/replay-checkcanrecord-deadlock
Startup times
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| ba152d2 | 346.75 ms | 440.34 ms | 93.59 ms |
| 03ae43b | 327.07 ms | 408.58 ms | 81.52 ms |
| 33731c0 | 351.60 ms | 422.48 ms | 70.88 ms |
| ace2fbf | 318.04 ms | 367.28 ms | 49.24 ms |
| 1922e86 | 319.80 ms | 359.07 ms | 39.27 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| ba152d2 | 0 B | 0 B | 0 B |
| 03ae43b | 0 B | 0 B | 0 B |
| 33731c0 | 0 B | 0 B | 0 B |
| ace2fbf | 0 B | 0 B | 0 B |
| 1922e86 | 0 B | 0 B | 0 B |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
feca2f0 to
dabe4b2
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dabe4b2. Configure here.
4c69937 to
70a764b
Compare
onScreenshotRecorded can run on the replay executor thread (PixelCopy masked-capture and emit paths). checkCanRecord -> pauseInternal acquires lifecycleLock — if another thread holds that lock and submits to the same single-threaded executor, we deadlock. When not already on the main thread, post checkCanRecord to the main looper so it never runs on the executor. On main thread, call directly to preserve existing synchronous behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
7c64d39 to
4af06f2
Compare
Holding lifecycleLock while calling awaitTermination() deadlocks if any executor task tries to acquire the same lock. Move shutdown after the lock is released to cut this edge. Ref: #5847 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
0xadam-brown
left a comment
There was a problem hiding this comment.
Thanks for the update 💯
Up to you whether you think it worthwhile to add tests to ReplayIntegrationTest verifying that we shutdown outside the lifecycleLock, and that we don't deadlock when invoking onScreenshotRecorded() from the replay executor path while lifecycleLock is held. Otherwise lgtm.
Verifies close() completes when an executor task is waiting on lifecycleLock, ensuring shutdown happens outside the lock. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Summary
onScreenshotRecordedcan run on the replay executor thread (PixelCopy masked-capture and emit paths).checkCanRecord()→pauseInternal()acquireslifecycleLock— if another thread holds that lock and submits to the same single-threaded executor, we deadlock.checkCanRecord()to the main looper so it never runs on the executor thread.This was a pre-existing issue introduced with SurfaceView capturing (masked-capture path calls
onScreenshotRecordedfrom executor atPixelCopyStrategy.kt:209), and is also relevant for PR #5808 which adds theemitLastScreenshotexecutor path.Test plan
onScreenshotRecorded pauses replay when rate-limited for sessionstest updated to idle looper./gradlew :sentry-android-replay:testReleaseUnitTest --tests="*ReplayIntegrationTest*"passes🤖 Generated with Claude Code